//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    tabbar: {},
    toView: "",
    motto: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    gps: null,
    deviceList: null,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 100,
    banner_list: [
      {
        "banner": [
          {
            "pic_url": "http://img09.yiguoimg.com/e/ad/2016/160825/585749448986042649_800x400.jpg",
          },
          {
            "pic_url": "http://img11.yiguoimg.com/e/ad/2016/160927/585749449690947899_800x400.jpg",
          }
        ]
      },
      {
        "banner": []
      }
    ],
    lists: [
      {
        "id": '01',
        "title": "居然之家大兴店冲电站",
        "type":"限时免费停车",
        "subTitle": "出示带日期的充电记录出示带日期的充电记录出示带日期的充电记录",
        "money":'1.46',
        "distance":'2.67'
      },
      {
        "id": '02',
        "title": "嘉悦广场充电站",
        "type": "免费停车",
        "subTitle": "出示带日期的充电记录出示带日期的充电记录出示带日期的充电记录",
        "money": '1.38',
        "distance": '6.29'
      },
      {
        "id": '03',
        "title": "嘉悦广场充电站",
        "type": "免费停车",
        "subTitle": "出示带日期的充电记录出示带日期的充电记录出示带日期的充电记录",
        "money": '1.38',
        "distance": '6.29'
      }
    ],
  },
  //详情页跳转
  listDetail: function (e) {
    var lookid = e.currentTarget.dataset;
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      //url: "/pages/detail/index"
      url: "/pages/detail/index?id=" + lookid.id
    })
  },
  //事件处理函数
  showDetail(e) {
    console.log(e);
    let obj = e.currentTarget.dataset.obj;

    wx.navigateTo({
      url: "./detail/index?id=" + obj.id
    })
  },

  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  scanCode: function () {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res)
      }
    })
  },

  getNearbyDeviceList: function(gps) {
    let that = this;

    wx.showLoading({
      title: '通讯中...',
    });
    wx.request({
      url: app.globalData.serverUrl + "/user/getUserByCode",
      method: "post",
      data: gps,
      success: function (resp) {
        wx.hideLoading();
        if (resp.data.success) {
          that.setData({
            deviceList: resp.data.data
          })
        }
      },
      fail: function (error) {
        wx.hideLoading();
        wx.showToast({
          title: "通讯异常: " + JSON.stringify(error),
          icon: 'none',
        });
      }
    });
  },

  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        // const latitude = res.latitude
        // const longitude = res.longitude
        // const speed = res.speed
        // const accuracy = res.accuracy
        that.setData({
          gps: res
        });

        that.getNearbyDeviceList(res);
      }
    });

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
